<?php

namespace Ignus\View\Extension;

use Ignus\App;
use Ignus\Localize;
use Ignus\Http;

use Ignus\View\Extension;
use Ignus\View\Functions\Method;

class Integrate extends Extension
{
    public function getName()
    {
        return 'integrate';
    }

    public function getFunctions()
    {
        return array(
            'l'    => new Method($this, 'localize'),
            'url'  => new Method($this, 'getAbsoluteUrl'),
            'path' => new Method($this, 'getUrl'),
        );
    }

    public function getUrl($controller, $action, array $params = array())
    {
        return App::getRouter()->assemble($controller, $action, $params);
    }

    public function getAbsoluteUrl($controller, $action, array $params = array())
    {
        $path = $this->getUrl($controller, $action, $params);
        return Http::getUrl() . $path;
    }

    public function localize($message)
    {
        $file = 'default';
        if (strpos($message, '/') !== false)
            list($file, $message) = explode('/', $message, 2);

        if (($argsNumber = func_num_args()) == 1)
            return Localize::get($file, $message);

        $args = array_slice(func_get_args(), 1);
        if ($argsNumber == 2 && is_array($args[0]))
            return Localize::replace($file, $message, $args[0]);

        array_unshift($args, $file, $message);
        return call_user_func_array('\Ignus\Localize::format', $args);
    }
}
